package cetcbigdata.da.service.hainan;

import cetcbigdata.da.base.Base;
import cetcbigdata.da.service.hainan.util.AllInfo;
import com.alibaba.datax.common.plugin.RecordSender;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

/**
 * 政务公开
 * "一窗受理"事项清单
 */

public class ZongHeShixiangList extends Base {
    @Override
    public void getData(RecordSender recordSender, List<Object> fieldsList) {

    }

    @Override
    public JSONObject getDataDemo() {
        return null;
    }

    public JSONObject parseListPage(){
        JSONArray cfg_dept_idList = new JSONArray();
        cfg_dept_idList.add("HZ54552869fae9d9016a060180ae132b");
        cfg_dept_idList.add("HZ54552869fae9d9016a060476b21331");
        cfg_dept_idList.add("HZ54552869fae9d9016a0605583d1338");
        cfg_dept_idList.add("HZ54552869fae9d9016a06088a851347");
        for (Object o : cfg_dept_idList){
            String cfg_dept_id = (String) o;
            //创建获取详情页信息的对象
            AllInfo allInfo = new AllInfo();
            //列表当前页
            int currentPage = 1;
            //列表总页数
            int pags = 0;
            //遍历列表每一页
            while (currentPage != pags) {
                String url = "https://wssp.hainan.gov.cn/wssp3/zwgk/checklist";
                JSONObject header = new JSONObject();
                JSONObject params = new JSONObject();
                params.put("dept", "");
                params.put("regionId", "HZ2881f4424539dd0142453c856b0025");
                params.put("taskType", "");
                params.put("isOnLine", "");
                params.put("taskName", "");
                params.put("pageNumber", currentPage);
                params.put("pageSize", "10");
                params.put("pdmsType", cfg_dept_id);
                params.put("qdCategory", "zhckSxQd");
                params.put("cfg_dept_id", cfg_dept_id);
                header.put("Referer", "https://wssp.hainan.gov.cn/hnwt/govpublic_detail?qdCategory=bjmspqd");
                header.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)");
                String listStrResponse = postFormReq(url, header, params);
                JSONObject listResponse = JSONObject.parseObject(listStrResponse);
                JSONArray deptList = listResponse.getJSONObject("data").getJSONArray("list");
                if (deptList != null && deptList.size()>0) {
                    for (Object o2 : deptList) {
                        JSONObject dept = (JSONObject) o2;
                        JSONArray deptResultList = new JSONArray();
                        JSONObject result = new JSONObject();
                        JSONArray subdatas = dept.getJSONArray("subdatas");
                        for (Object o1 : subdatas) {
                            JSONObject subdata = (JSONObject) o1;
                            //向详情页信息对象传入请求参数
                            allInfo.setId(subdata.getString("id"));
                            allInfo.setSourcekey(subdata.getString("sourcekey"));
                            //获取封装好的详情页数据
                            allInfo.init();
                            try {
                                deptResultList.add(allInfo.getAllInfo());
                            } catch (Exception e) {
                                break;
                            }
                        }

                        result.put("名称", dept.getString("taskname"));
                        result.put("选项", deptResultList);
                        System.out.println(currentPage+"\n"+result);
                        return processResult(
                                null,
                                null,
                                null,
                                allInfo.getResponce(),
                                result,
                                "海南省",
                                "政务公开-综合服务清单"
                        );
                    }
                }
                //更新当前页
                currentPage++;
                //更新总页数
                pags = listResponse.getJSONObject("data").getJSONObject("page").getInteger("pageCount") + 1;
            }
        }
        return null;
    }

    public static void main(String[] args) {
        ZongHeShixiangList zongHeShixiangList = new ZongHeShixiangList();
        zongHeShixiangList.parseListPage();
    }
}
